package cn.flying;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import cn.flying.data.core.processor.CompiledMapping;
import cn.flying.data.core.processor.JsonDataProcessor;
import cn.flying.data.entity.config.FieldMappingConfig;
import cn.flying.data.entity.config.FieldMappingConfig.FieldType;

/**
 * @author: admin
 * @date: 2025年09月29日 20:10
 * @version: 1.0
 */
public class Main {
    public static void main(String[] args) {

        JsonDataProcessor processor = new JsonDataProcessor();

        // 1. 获取映射配置
        List<CompiledMapping> mappings = new ArrayList<>();
        FieldMappingConfig config = new FieldMappingConfig();
        config.setSourceId(0L);
        config.setTargetTable("om_member");
        config.setJsonPath("$.id");
        config.setDbFieldName("id");
        config.setFieldType(FieldType.STRING);
        config.setFieldLength(10);
        config.setPrimaryKey(false);
        config.setRequired(false);
        config.setDefaultValue("");
        config.setDateFormat("");
        config.setValueTransformRule("");
        config.setValidationRule("");
        config.setFieldOrder(0);
        config.setActive(false);
        config.setCreatedTime(LocalDateTime.now());
        config.setUpdatedTime(LocalDateTime.now());
        CompiledMapping mapping = new CompiledMapping(config);
        mappings.add(mapping);

        config = new FieldMappingConfig();
        config.setSourceId(0L);
        config.setTargetTable("om_member");
        config.setJsonPath("$.highFWords");
        config.setDbFieldName("highFWords");
        config.setFieldType(FieldType.STRING);
        config.setFieldLength(10);
        config.setPrimaryKey(false);
        config.setRequired(false);
        config.setDefaultValue("");
        config.setDateFormat("");
        config.setValueTransformRule("");
        config.setValidationRule("");
        config.setFieldOrder(0);
        config.setActive(false);
        config.setCreatedTime(LocalDateTime.now());
        config.setUpdatedTime(LocalDateTime.now());
        mapping = new CompiledMapping(config);
        mappings.add(mapping);

        config = new FieldMappingConfig();
        config.setSourceId(0L);
        config.setTargetTable("om_member");
        config.setJsonPath("$.count");
        config.setDbFieldName("count");
        config.setFieldType(FieldType.STRING);
        config.setFieldLength(10);
        config.setPrimaryKey(false);
        config.setRequired(false);
        config.setDefaultValue("");
        config.setDateFormat("");
        config.setValueTransformRule("");
        config.setValidationRule("");
        config.setFieldOrder(0);
        config.setActive(false);
        config.setCreatedTime(LocalDateTime.now());
        config.setUpdatedTime(LocalDateTime.now());
        mapping = new CompiledMapping(config);
        mappings.add(mapping);

        config = new FieldMappingConfig();
        config.setSourceId(0L);
        config.setTargetTable("om_member");
        config.setJsonPath("$.pos");
        config.setDbFieldName("pos");
        config.setFieldType(FieldType.STRING);
        config.setFieldLength(10);
        config.setPrimaryKey(false);
        config.setRequired(false);
        config.setDefaultValue("");
        config.setDateFormat("");
        config.setValueTransformRule("");
        config.setValidationRule("");
        config.setFieldOrder(0);
        config.setActive(false);
        config.setCreatedTime(LocalDateTime.now());
        config.setUpdatedTime(LocalDateTime.now());
        mapping = new CompiledMapping(config);
        mappings.add(mapping);

        config = new FieldMappingConfig();
        config.setSourceId(0L);
        config.setTargetTable("om_member");
        config.setJsonPath("$.section");
        config.setDbFieldName("section");
        config.setFieldType(FieldType.STRING);
        config.setFieldLength(10);
        config.setPrimaryKey(false);
        config.setRequired(false);
        config.setDefaultValue("");
        config.setDateFormat("");
        config.setValueTransformRule("");
        config.setValidationRule("");
        config.setFieldOrder(0);
        config.setActive(false);
        config.setCreatedTime(LocalDateTime.now());
        config.setUpdatedTime(LocalDateTime.now());
        mapping = new CompiledMapping(config);
        mappings.add(mapping);

        String json = "{\n" +
                "  \"id\":24,\n" +
                "  \"highFWords\":\"报警\",\n" +
                "  \"count\":46266,\n" +
                "  \"pos\":5346345,\n" +
                "  \"section\":\"10000以上\"\n" +
                "}";

        // 2. 解析JSON为数据库记录
        Map<String, Object> dbRecord = processor.parseToDbRecord(json, mappings);

        System.out.println(dbRecord);

        String sql = buildInsertSql(dbRecord, "om_member");
        System.out.println(sql);
    }

    /**
     * 构建插入SQL
     */
    private static String buildInsertSql(Map<String, Object> record, String tableName) {
        String columns = String.join(", ", record.keySet());
        String placeholders = String.join(", ", Collections.nCopies(record.size(), "?"));

        return String.format("INSERT INTO %s (%s) VALUES (%s)", tableName, columns, placeholders);
    }
}